/**
 * @file shell_port.c
 * @author Letter (NevermindZZT@gmail.com)
 * @brief 
 * @version 0.1
 * @date 2019-02-22
 * 
 * @copyright (c) 2019 Letter
 * 
 */

#include "cmsis_os2.h"
#include "lettershell/shell.h"
#include "shell_log.h"
#include "FreeRTOS.h"
#include "task.h"
#include "bsp_uart.h"
#include "global.h"


Shell shell;
char shellBuffer[4096 + 1];



/**
 * @brief 用户shell写
 * 
 * @param data 数据
 * @param len 数据长度
 * 
 * @return short 实际写入的数据长度
 */
int16_t userShellWrite(char *data, uint16_t len)
{
    return bsp_uart_itwrite(debug_uart_id, (uint8_t *)data, len); 
}


/**
 * @brief 用户shell读
 * 
 * @param data 数据
 * @param len 数据长度
 * 
 * @return short 实际读取到
 */

int16_t userShellRead(char *data, uint16_t len)
{
    return bsp_uart_read(debug_uart_id, data, len);    
}


/**
 * @brief 延时函数  
 * @param  ms         延时事件，单位: ms
 */
void userShellDelay(uint16_t ms)
{
    vTaskDelay(ms);
}

/**
 * @brief 用户shell上锁
 * 
 * @param shell shell
 * 
 * @return int 0
 */
int userShellLock(Shell *shell)
{
    // xSemaphoreTakeRecursive(shellMutex, portMAX_DELAY);
    return 0;
}

/**
 * @brief 用户shell解锁
 * 
 * @param shell shell
 * 
 * @return int 0
 */
int userShellUnlock(Shell *shell)
{
    // xSemaphoreGiveRecursive(shellMutex);
    return 0;
}


/**
 * @brief 用户shell初始化
 * 
 */
void userShellInit(void)
{
    // shellMutex = xSemaphoreCreateMutex();
    shell.write = userShellWrite;
    shell.read = userShellRead;
    shell.delay_ms = userShellDelay;
    // shell.lock = userShellLock;
    // shell.unlock = userShellUnlock;
    shellInit(&shell, shellBuffer, sizeof(shellBuffer));
    

    /* 创建线程 */
    if (xTaskCreate(shellTask, "shell", TASKSTACK_SHELL, &shell, TASKPRI_SHELL, NULL) != true)
    {
        printf("shell task creat failed");
    }
}



